using Nop.Core;
using Nop.Core.Plugins;
using Nop.Plugin.DiscountRules.Customers.Services;
using Nop.Services.Customers;
using Nop.Services.Discounts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nop.Plugin.DiscountRules.Customers
{
	/// <summary>
	/// Assign to customer discount plugin
	/// </summary>
	public class CustomerDiscountRequirementRule : BasePlugin, IDiscountRequirementRule
    {
		private readonly IDiscountService _discountService;
		private readonly ICustomerService _customerService;
		private readonly IRequirementCustomerService _requirementCustomerService;

		public CustomerDiscountRequirementRule(IDiscountService disctountService, 
									ICustomerService customerService,
									IRequirementCustomerService discountCustomerService)
		{
			_discountService = disctountService;
			_customerService = customerService;
			_requirementCustomerService = discountCustomerService;
		}

		public bool CheckRequirement(CheckDiscountRequirementRequest request)
		{
			if (request == null)
				throw new ArgumentNullException("request");

			if (request.DiscountRequirement == null)
				throw new NopException("Discount requirement is not set");

			if (request.Customer == null)
				return false;

			var customer = _requirementCustomerService.GetRequirementCustomer(
																request.Customer.Id, 
																request.DiscountRequirement.Id);

			return customer != null;
		}

		public string GetConfigurationUrl(int discountId, int? discountRequirementId)
		{
			//configured in RouteProvider.cs
			string result = "Plugins/DiscountRulesCustomers/Configure/?discountId=" + discountId;
			if (discountRequirementId.HasValue)
				result += string.Format("&discountRequirementId={0}", discountRequirementId.Value);
			return result;
		}
	}
}
